package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Visitors;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentService commentService;

    /**
     * 发布动态
     */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询我的动态
     */
    @GetMapping("/all")
    public ResponseEntity findMyMovement(Long userId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {

        PageResult pr = movementService.findMyMovement(userId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 推荐动态
     */
    @GetMapping("/recommend")
    public ResponseEntity recommendMove(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 好友动态
     */
    @GetMapping
    public ResponseEntity friendMove(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.friendMove(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 动态详情
     * id ：动态的id
     */
    @GetMapping("/{id}")
    public ResponseEntity movementDetail(@PathVariable("id") String id) {
        MovementsVo vo = movementService.movementDetail(id);
        return ResponseEntity.ok(vo);
    }

    /**
     * 点赞
     *
     * @param id:动态的id
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String id) {
        Integer likeCount = commentService.likeMovement(id);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     * id：动态的id
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String id){
        Integer count = commentService.dislikeMovement(id);
        return ResponseEntity.ok(count);
    }

    /**
     * 动态喜欢
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String id){
        Integer count = commentService.loveMovement(id);
        return ResponseEntity.ok(count);
    }

    /**
     * 动态取消喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String id){
        Integer count = commentService.unloveMovement(id);
        return ResponseEntity.ok(count);
    }

    /**
     * 查看访客列表
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorsVo> list  =  movementService.visitors();
        return ResponseEntity.ok(list);
    }
}
